<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ include file="/common/common.jsp"%>
<%@ include file="/common/commonJs.jsp"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<script type="text/javascript"
	src="${ctx}/js/comm.js"></script>
<script type="text/javascript"
	src="${ctx}/js/jquery-1.8.0.min.js"></script>
<script>
	//
	var javaCodeTemplate = commLoad("${ctx}/createOperates/createJavaCode.xsl");
	var javaCodeContent = commLoad("${ctx}/createOperates/operates.xml");
	function addColumn(obj) {
// 		var rowXmlDoc = commLoadXML(javaCodeContent);
		var resultDocument = null;
 		try {
			if (document.implementation && document.implementation.createDocument) {//webkit
				xsltProcessor = new XSLTProcessor();
				//引入样式表
				xsltProcessor.importStylesheet(javaCodeTemplate);
				//resultDocument = xsltProcessor.transformToFragment(rowXmlDoc, document);
				//转换文档
				var resultDoc = xsltProcessor.transformToDocument(javaCodeContent, document);
			    var resultDocument = changeXmlDoc2Str(resultDoc);
			}
		} catch (e) {//ie
			console.log(e);
			//转换文档
			resultDocument = javaCodeContent.transformNode(javaCodeTemplate);
		}
		var i = obj.parentNode.parentNode.rowIndex;
		var tbody = obj.parentNode.parentNode.parentNode;
// 		alert(resultDocument);
		$("#sqlOut").text(resultDocument);
// 		$(tbody).append(resultDocument);
	}

	function callback(args, datasXmlDoc) {
		var showStr = null;
		var datasXslDoc = commLoad("${ctx}//createEntity/createEntityUI.xsl");
		
		var xslStr =  changeXmlDoc2Str(datasXslDoc);
		try {
			  showStr=datasXmlDoc.transformNode(datasXslDoc);
			  document.getElementById("entityContent").innerHTML=showStr;
			  alert(showStr);
			  }
		catch(e){
			showStr = (new XMLSerializer).serializeToString(xml2New(datasXmlDoc,datasXslDoc));
			var divContent = document.getElementById("entityContent");
			divContent.innerHTML = showStr;
		  }
		alert(showStr);
		//alert($($("#id.dataType").val()+"#name.dataType").val());
	}
	
	function removeColumn(obj) {
		//alert(1);
		var tbody = obj.parentNode.parentNode.parentNode;
		var tbody = obj.parentNode.parentNode.parentNode;
		var row = obj.parentNode.parentNode;
		if(row.rowIndex == 1 || row.rowIndex == 2){
			alert("表至少有一个属性！");
			return;
		}
		tbody.removeChild(row);
	}

	/**
	*
	* 收集数据
	*
	**/
	function pickUpData() {
		var table = $("#table").val();
		var tableName = $("#tableName").val();
		var pks = $("input[name='pk']");
		var columnNames = $("input[name='columnName']");
		var dataTypes = $("input[name='dataType']");
		var tableStrutStr = " create table " + tableName + " ( ";
		for ( var i = 0; i < columnNames.length; i++) {
			tableStrutStr += columnNames[i].value + " " + dataTypes[i].value;
			if(pks[i].checked){
				tableStrutStr += " primary key ";
			}
			
			if (i + 1 < columnNames.length)
				tableStrutStr += ",";
		}
		tableStrutStr += " ) ";

		console.log(tableStrutStr);
		
		return tableStrutStr;
	}

	/**
	 ** 查检主键支持
	 */
	function checkPkNumSupported() {
		var isSupportedParmaryKeyNum = true;
		if ($("input[name='pk']:checked").length > 2) {
			isSupportedParmaryKeyNum = false;
			alert("程序暂时支持最多2主键!");
		}
		return isSupportedParmaryKeyNum;
	}

	function save() {
		if (!checkPkNumSupported())
			return;
		$("#sqlOut").val(pickUpData());
	}

	function createTableStructXml() {
		var table = document.getElementById("table");
		var tableName = document.getElementById("tableName");
		var pks = document.getElementsByName("pk");
		var columnNames = document.getElementsByName("columnName");
		//alert(columnNames.length);
		//alert(table.childNodes);

		var dataTypes = document.getElementsByName("dataType");
		var tableStructXml = "<table name='"+tableName.value+"'> ";
		for ( var i = 0; i <= columnNames.length - 1; i++) {
			tableStructXml = tableStructXml + "<column name = '"
					+ columnNames[i].value + "' " + "dataType = '"
					+ dataTypes[i].value + "' " + "isPk = ";
			if (pks[i].checked == true)
				tableStructXml = tableStructXml + "'" + true + "' " + " />";
			else
				tableStructXml = tableStructXml + "'" + false + "' " + " />";
		}
		tableStructXml = tableStructXml + " </table>";
		//alert(tableStructXml);
		return tableStructXml;
	}

	function rebackXmlDoc(xmlStr) {
		var oParser = new DOMParser();
		var retDom = oParser.parseFromString(xmlStr, "text/xml");
		return retDom;
	}

	function changeXmlDoc2Str(doc) {
		var xml = (new XMLSerializer()).serializeToString(doc);
		return xml;
	}

	function sendTableStructXml() {
		var xmlStr = createTableStructXml();
		var xmlDoc = rebackXmlDoc(xmlStr);
		var xml = changeXmlDoc2Str(xmlDoc);
		alert(xml);
		var url = "${ctx}/dataCreate.action?index=1";
// 		commHttpSend(url, xml, true, "callback", null);
		//alert(1);
	}

	function createTableStrutXml() {
		var tableCreateSql = pickUpData();

	}
</script>
<title>Insert title here</title>
</head>
<body>
	<!-- <table border="1" id="table">
		<tbody>
			<tr>
				<td align="center">表名</td>
				<td colspan="4"><input type="text" id="tableName"></input>
				</td>
			</tr>
			<tr>
				<td align="center">主键</td>
				<td align="center">列名</td>
				<td><input type="button" value="+" onclick="addColumn(this);"></input>
				</td>
				<td><input type="button" value="-"
					onclick="removeColumn(this);"></input>
				</td>
				<td align="center">数据类型</td>
			</tr>
			<tr>
				<td><input type="checkbox" id="pk" name="pk"></input>
				</td>
				<td><input type="text" id="columnName" name="columnName"></input>
				</td>
				<td><input type="button" value="+" onclick="addColumn(this);"></input>
				</td>
				<td><input type="button" value="-"
					onclick="removeColumn(this);"></input>
				</td>
				<td><input type="text" id="dataType" name="dataType"></input>
				</td>
			</tr>
		</tbody>
	</table> -->
	<input type="button" value="+" onclick="addColumn(this);"></input>
	
	<div id="tableContent">
		<table>
			<tr>
				<td><input type="button" onclick="pickUpData()" value="确定"></input>
				</td>
				<td><input type="button" onclick="save()" value="生成"></input>
				</td>
				<td><input type="button" onclick="createTableStrutXml()"
					value="保存"></input>
				</td>
				<td><input type="button" onclick="sendTableStructXml()"
					value="发送"></input>
				</td>
			</tr>
		</table>
	</div>
	<div id="entityContent"></div>
	<div>
		<textarea style="width:500px;height:500px;" id="sqlOut"></textarea>
	</div>
</body>
</html>